package org.wawa.tools.codegenerator.utils;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Map;

/**
 * @author kevin
 * @date 2020/06/01
 */
@Slf4j
public class GenFreemarkerUtil {

    public void execGen(String templateDir, String templatePath, Map<String, Object> dataMap, Writer out) {
        try {
            Configuration configuration = new Configuration(Configuration.VERSION_2_3_30);
            configuration.setDirectoryForTemplateLoading(new File(templateDir));
            configuration.setDefaultEncoding("UTF-8");
            Template template = configuration.getTemplate(templatePath);
            template.process(dataMap, out);
        } catch (Exception e) {
            log.error("生成freemarker模板的代码出错", e);
        }
    }
}
